// pages/allPolicy/allPolicy.js
const app = getApp();
var host = app.globalData.host;
var utils = app.globalData.utils;
var self = null;

Page({
  data: {
    themeColor: '',
    defaultColor: '',
    borderColor: '',
    schoolId: '',
    list: [],
    pageNumber: 1,
    pageSize: 0,
    totalPage: 0,
    lastPage: true,
    keyword: '',
    showFilter: false
  },
  onLoad: function (options) {
    self = this;
    self.getStyle();
    self.init();
    self.getSchools();
  },
  getStyle() {
    self.setData({
      themeColor: app.globalData.themeColor,
      defaultColor: app.globalData.defaultColor,
      borderColor: app.globalData.borderColor
    })
  },
  init() {
    var url = host + '/lx/mini/school/policy';
    var params = {
      schoolId: self.data.schoolId,
      keyword: self.data.keyword
    };
    var success = function (res) {
      console.log(res);
      self.setData({
        list: res.info.list,
        pageNumber: res.info.pageNumber,
        pageSize: res.info.pageSize,
        totalPage: res.info.totalPage,
        lastPage: res.info.lastPage
      })
    }
    var fail = function () { };
    var complete = function () { };
    utils.request(url, params, success, fail, complete);
  },
  getSchools() {
    var url = host + '/lx/mini/school/list';
    var params = {
    };
    var success = function (res) {
      console.log(res);
      self.setData({
        schoolArray: res.info.list
      })
    }
    var fail = function () { };
    var complete = function () { };
    utils.request(url, params, success, fail, complete);
  },
  goInfo(event) {
    var id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/app/schools/policyInfo/policyInfo?policyId=' + id
    })
  },
  search(event) {
    self.init();
  },
  clearKeyword() {
    self.setData({
      keyword: ''
    })
    self.init();
  },
  showFilter(event) {
    self.setData({
      showFilter: !self.data.showFilter
    })
  },
  selectSchool(e) {
    var schoolId = e.currentTarget.dataset.schoolid;
    var filter = self.data.filter;
    if (self.data.schoolId === schoolId) {
      self.setData({
        schoolId: ''
      })
    } else {
      self.setData({
        schoolId: schoolId
      })
    }
  },
  goFilter(e) {
    self.init();
    self.setData({
      showFilter: false
    })
  },
  onReachBottom() {
    if (self.data.lastPage) {
      return;
    }
    var url = host + '/lx/mini/school/policy';
    var params = {
      schoolId: self.data.schoolId,
      keyword: self.data.keyword,
      page: self.data.pageNumber + 1
    };
    var success = function (res) {
      console.log(res);
      var list = self.data.list.concat(res.info.list);
      self.setData({
        list: list,
        pageNumber: res.info.pageNumber,
        pageSize: res.info.pageSize,
        totalPage: res.info.totalPage,
        lastPage: res.info.lastPage
      })
    }
    var fail = function () { };
    var complete = function () { };
    utils.request(url, params, success, fail, complete);
  },
  onKeywordChange(e) {
    self.setData({
      keyword: e.detail.value
    })
  },
  goHome(e) {
    wx.reLaunch({
      url: '/pages/app/schools/index/index'
    })
  }
})